package cn.wsalix.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

import cn.wsalix.admin.entity.SysMenu;
import cn.wsalix.admin.service.MenuService;
import cn.wsalix.admin.service.UserService;

@Component("titleInterceptor")
public class TitleInterceptor extends HandlerInterceptorAdapter {
	private static final Logger logger = LoggerFactory
			.getLogger(TitleInterceptor.class);
	@Autowired
	private MenuService menuService;
	@Autowired
	private UserService userService;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String currUrl = request.getRequestURI().substring(
				request.getContextPath().length());
		if (currUrl.endsWith(".do")) {
			SysMenu menu = menuService.findByUrl(currUrl);
			request.setAttribute("currMenu", menu);
		}
		if (handler instanceof HandlerMethod) {
			/*
			 * HandlerMethod handlerMethod = (HandlerMethod) handler; Menu
			 * annotation = handlerMethod.getMethodAnnotation(Menu.class); if
			 * (annotation != null) { SysMenu menu =
			 * menuService.findByCode(annotation.code());
			 * request.setAttribute("currMenu", menu); }
			 * 
			 * Method method = handlerMethod.getMethod(); MenuPath annotation =
			 * method.getAnnotation(MenuPath.class); if (annotation != null) {
			 * HttpSession session = request.getSession(true);
			 * session.setAttribute("backUrl", module + annotation.path()); }
			 */
		}

		/*
		 * HttpSession session = request.getSession(true); Deque<String> backUrl
		 * = (Deque<String>) session.getAttribute("backUrl"); boolean isExist =
		 * true; if (backUrl == null) { isExist = false; backUrl = new
		 * ArrayDeque<String>(5); } if (currReqUrl.endsWith(".back")) { if
		 * (backUrl.size() > 0) { String popUrl = backUrl.pop(); //
		 * response.sendRedirect(popUrl); return true; } } else { if
		 * (backUrl.size() > 10) { backUrl.peekFirst(); }
		 * backUrl.push(currReqUrl); } if (isExist) {
		 * session.removeAttribute("backUrl"); } session.setAttribute("backUrl",
		 * backUrl);
		 */
		request.setAttribute("url", request.getRequestURI());
		request.setAttribute("currUrl", currUrl);
		logger.info(request.getRequestURI());
		logger.info("===========HandlerInterceptor1 preHandle:["
				+ request.getMethod() + "]" + request.getRequestURI());
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (modelAndView != null) {
			Map<String, Object> model = modelAndView.getModel();
			String viewName = modelAndView.getViewName();
			// modelAndView.addAllObjects(modelMap)
			HttpSession requestOne = request.getSession(true);
			requestOne.setAttribute("requestOne", model);
		}

		/*
		 * request.setAttribute("templetDir", Global.templetPath + "/admin");
		 * request.setAttribute("siteTempletDir", Global.templetPath + "/site");
		 */
		/*
		 * String appPath = request.getContextPath();
		 * request.setAttribute("appPath", appPath);
		 */
		if (handler instanceof ParameterizableViewController) {

		}
		logger.info("===========HandlerInterceptor1 postHandle");
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		logger.info("===========HandlerInterceptor1 afterCompletion");
	}

}
